package com.rq.base.net;

import android.text.TextUtils;

import com.rq.base.net.impl.TagGetter;
import com.rq.base.net.impl.UrlImpl;
import com.rq.base.util.LOG;


public class RequestType {
  private UrlImpl type;
  private int pageNum = 0;
  public static final String PAGE_NUM_TAG = "pageNo";
  public static final int PAGE_SIZE = 20;//单页一次加载数量
  private int respCode;
  private String respMsg;
  private Object more;

  public UrlImpl getType() {
    return type;
  }

  public RequestType setRespMsg(String respMsg) {
    this.respMsg = respMsg;
    return this;
  }

  public RequestType(UrlImpl type) {
    this.type = type;
    if (type.getMore() != null) {
      this.more = type.getMore();
    }
  }

  public RequestType setPageNum(int page) {
    this.pageNum = page;
    return this;
  }

  public Object getMore() {
    return more;
  }


  public int getRespCode() {
    return respCode;
  }

  public String getErrorMsg() {
    return respMsg;
  }

  public RequestType more(Object more) {
    this.more = more;
    return this;
  }

//    public boolean isMore(TagGetter getter, Object more) {
//        if (getter == null) {
//            return false;
//        }
//        String tag = getter.getTag();
//        if (TextUtils.equals(tag, type.getTag())) {
//            if (more == this.more) {
//                return true;
//            } else if (more != null) {
//                return more.equals(this.more);
//            } else {
//                return this.more == null;
//            }
//        }
//        if (tag != null) {
//            tag = tag.substring(1 + tag.lastIndexOf("/"));
//        }
//        if (more == null) {
//            return TextUtils.equals(tag, type.getTag());
//        } else {
//            return TextUtils.equals(tag, type.getTag()) && this.more.equals(more);
//        }
//    }

  public boolean is(TagGetter getter) {
    if (getter == null) {
      return false;
    }
    String tag = getter.getTag();
    LOG.e("RequestType", "is.tag:" + tag);
    LOG.e("RequestType", "is.type.getTag():" + type.getTag());
    if (TextUtils.equals(tag, type.getTag())) {
      return true;
    }
    return TextUtils.equals(tag, type.getTag());
  }

  public int getPageNum() {
    return pageNum;
  }

  public boolean isNetError() {
    return respCode < 0;
  }

  public RequestType with(String respCode, String msg) {
    try {
      return this.with(Integer.parseInt(respCode), msg);
    } catch (Exception e) {
      this.respCode = 0;
      this.respMsg = msg;
      e.printStackTrace();
    }
    return this;
  }

  public RequestType with(int respCode, String msg) {
    this.respMsg = msg;
    this.respCode = respCode;
    return this;
  }

  @Override
  public String toString() {
    return "RequestType{" + "type='" + type + '\'' + ", pageNum=" + pageNum + ", respCode=" + respCode + ", respMsg='" + respMsg + '\'' + ", more=" + more + '}';
  }

  public boolean isFirst() {
    LOG.e("http", "isFirst=======  " + pageNum);

    return pageNum == 1 || pageNum == 0;
  }

}